#*--------------------------------------------------------------------------------
# DESCRIPTION: Creating Appendix Figure G5
#
#--------------------------------------------------------------------------------#

install.packages(c("fect","haven", "fixest", "ggplot2", "extrafont", "dplyr", "stringr", "flextable", "officer"))

library(dplyr)
library(extrafont)
library(fect)
library(fixest)
library(ggplot2)
library(haven)
library(stringr)
library(flextable)
library(officer)

font_import()
loadfonts(device = "win")

##############################################################################################################
##SET RELEVANT WORKING DIRECTORY
setwd("...") 

##############################################################################################################
##PREPARE DATA
data <- read_dta("data/clean/country_year_panel.dta")
df.use <- data[!is.na(data$growth_shock_mpd),]
df.use <- data.frame(df.use)
df.use <- get.cohort(data = df.use, D = "growth_shock_mpd",index = c("panelid","year"))

##############################################################################################################
##RUN MODELS
model.fect <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                   method = "fe", index = c("panelid","year"), se = TRUE, nboots = 200, alpha = 0.05, 
                   parallel = TRUE, seed = 1234, force = "two-way", group = "Cohort")

model.ife <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                  index = c("panelid","year"), force = "two-way", method = "ife", CV = TRUE, r = c(0, 4), alpha = 0.05, 
                  se = TRUE, nboots = 200, parallel = TRUE, seed = 1234, group = "Cohort")

model.mc <- fect(trust_govt_wo ~ growth_shock_mpd, data = df.use, 
                 index = c("panelid","year"), force = "two-way", method = "mc", CV = TRUE, alpha = 0.05, 
                 se = TRUE, nboots = 200, parallel = TRUE, seed = 1234, group = "Cohort")

##############################################################################################################
##ENTERING TREATMENT
ci_bounds <- model.fect[["est.group.output"]][["Cohort:2009"]][["att.on.bound"]]
att <- model.fect[["group.output"]][["Cohort:2009"]][["att.on"]]
time <- model.fect[["group.output"]][["Cohort:2009"]][["time.on"]]
fect.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "FE"
)

ci_bounds <- model.ife[["est.group.output"]][["Cohort:2009"]][["att.on.bound"]]
att <- model.ife[["group.output"]][["Cohort:2009"]][["att.on"]]
time <- model.ife[["group.output"]][["Cohort:2009"]][["time.on"]]
ife.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "IFE"
)

ci_bounds <- model.mc[["est.group.output"]][["Cohort:2009"]][["att.on.bound"]]
att <- model.mc[["group.output"]][["Cohort:2009"]][["att.on"]]
time <- model.mc[["group.output"]][["Cohort:2009"]][["time.on"]]
mc.output <- data.frame(
  Time = time,
  ATT = att,
  CI.lower = ci_bounds[, 1],
  CI.upper = ci_bounds[, 2],
  Model = "MC"
)

output <- rbind(fect.output, ife.output, mc.output)

#############################################################################################################################

######################
##Create Figure G5##
######################

dpi=600    #pixels per square inch
png("output/figures/appendix_g/figure_g5.png", width=6*dpi, height=5*dpi, res=dpi)
figure_g5 <- ggplot(output, aes(x = Time, y = ATT, group = Model)) +
  geom_hline(yintercept = 0, colour = "firebrick4") +
  geom_vline(xintercept = 0, colour = "firebrick4", linetype = 'dashed') +
  geom_errorbar(aes(ymin = CI.lower,
                    ymax = CI.upper, color = Model), position = position_dodge(width=0.5)) + 
  geom_point(aes(shape = Model, fill = Model),  size=1.75, color = "black", position = position_dodge(width=0.5)) + 
  theme(text = element_text(family = "Palatino"),
        panel.background = element_rect(fill = 'white', color = 'black'),
        panel.grid.major = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        panel.grid.minor = element_line(color = gray(1/4), size = 0.2, linetype = 'dotted'),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        axis.text=element_text(size=12), 
        axis.title=element_text(size=12),
        title=element_text(size=13),
        plot.caption = element_text(hjust=0)) + 
  coord_cartesian(xlim=c(-4,5)) + ylim(-0.3,0.2) + scale_x_continuous(breaks=seq(-4, 5, 1)) + 
  ylab("Effect on Trust in Government") + 
  xlab("Time to 2008 Financial Crisis") +
  scale_color_manual(values=c('firebrick4','darkgrey','steelblue4')) +
  scale_fill_manual(values=c('firebrick4','darkgrey','steelblue4')) +
  scale_shape_manual(values = c(21, 23, 22)) + guides(col = "none")
figure_g5
dev.off()
